﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QualityRpt.Service;
using QualityRpt.Utils;
using System.Data;

namespace QualityRPT
{
    public partial class Joblist : System.Web.UI.Page
    {
        public string strPage = string.Empty;
        public string PagerHtml = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string stract = Request.Params["act"];
                if (stract != null)
                {
                    string strid = Request.Params["id"];
                    DataRow row = ServiceInfoService.Get(Convert.ToInt64(strid));
                    if (stract == "pause")
                    {
                        row["DATA_STATUS"] = 4;
                        ServiceInfoService.Update(row);
                    }
                    else if (stract == "ctn")
                    {
                        row["DATA_STATUS"] = 0;
                        ServiceInfoService.Update(row);
                    }
                }
                PageUtils pu = new PageUtils();
                this.repeater_joblist.DataSource = ServiceInfoService.GetList(pu, "S.DATA_STATUS>=0");
                this.repeater_joblist.DataBind();
                PagerHtml = pu.GenStPagination("Joblist.aspx?pid=" + Request.Params["pid"] + "&cid=" + Request.Params["cid"] + "&p={page}");
            }
        }
        public string GetStatus(object objStatus)
        {
            string rt = "等待执行";
            if (objStatus != DBNull.Value)
            {
                int iStatus = Convert.ToInt32(objStatus);
				if (iStatus == 1) {
					rt = "执行中";
				} else if (iStatus == 2) {
					rt = "任务完成";
				} else if (iStatus == 3) {
					rt = "<span class='error'>任务失败</span>";
				} 
				else if (iStatus == 4) {
					rt = "正在停止";
				} else if (iStatus == 5) {
					rt = "已停止";
				} 
            }
            return rt;
        }
        public string GetJobType(object objType)
        {
			string rt = "合并抽取和分析";
            if (objType != DBNull.Value)
            {
                int iType = Convert.ToInt32(objType);
				if (iType == 1){
					rt = "合并抽取任务";
				}
				else if (iType == 2) {
					rt = "按日汇总任务";
				}
				else if (iType == 3) {
					rt = "漏读分析任务";
				}
				else if (iType == 4) {
					rt = "邻道干扰分析任务";
				}
				else if (iType == 5) {
					rt = "军车统计任务";
				}
				else if (iType == 11)
                {
                    rt = "标签统计任务";
                }
                else if (iType == 12)
                {
                    rt = "天线统计任务";
                }
                else if (iType == 13)
                {
                    rt = "IC卡统计任务";
                }
            }
            return rt;
        }
        public string GetRsStyle(object objType, object objStatus)
        {
            string rt = "visibility:hidden;";
            int iJType = Convert.ToInt32(objType);
            int iStatus = Convert.ToInt32(objStatus);
            if (iJType >=10 && iJType<20 && iStatus == 2)
            {
                rt = "visibility:visible;";
            }
            return rt;
        }
		public string GetJobUserName(object uid)
		{
			string rt = "自动策略";
			int iUid = 0;
			if (uid != DBNull.Value) {
				iUid = Convert.ToInt32 (uid);
				if (iUid > 0) {
					DataRow row = UserService.Get (iUid);
					rt = row ["USERNAME"].ToString ();
				}
			}
			return rt;
		}
        public string GetPauseLink(object objStatus,object objId)
        {
            string rt = string.Empty;
            if (objStatus != null && objStatus != DBNull.Value)
            {
                string strStatus = objStatus.ToString();
                if (strStatus.Trim() != null)
                {
                    int iStatus = Convert.ToInt32(strStatus);
                    if (iStatus == 1)//正在执行的可以暂停
                    {
                        rt = "<a href='Joblist.aspx?act=pause&id="+objId.ToString()+"&pid="+Request.Params["pid"]+"&cid="+Request.Params["cid"]+"'>停止</a>";
                    }
                    else if (iStatus == 5)//可以继续
                    {
                        rt = "<a href='Joblist.aspx?act=ctn&id=" + objId.ToString() + "&pid=" + Request.Params["pid"] + "&cid=" + Request.Params["cid"] + "'>重新开始</a>";
                    }
                }
            }
            return rt;
        }
    }
}